package dcc;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *
 * @author Atuan
 */
public class Rle implements CompressionAlgorithm{

	
	
    @Override
	public void compress(InputStream in, OutputStream out) throws IOException {
    	byte cur[] = new byte[1];
        byte prev[] = new byte[1];
        byte tmp[] = new byte[1];
        int cnt = 0;
        int  cont;

        cont = in.read(cur, 0, 1);
        prev[0] = (byte) ~cur[0];
        while (cont == 1){
            if (prev[0] != cur[0]){
                if (cnt == 0){
                    /* znaki obok siebie rozne wrzuc do pliku wyjsciowego */
                    out.write(cur, 0, 1);
                } else {
                    /* skonczyla sie sekwencja powtarzajcych sie znakow */
                    cnt--;
                    out.write(prev, 0, 1);
//                    tmp[0] = (byte) cnt;
//                    out.write(tmp, 0, 1);
                    out.write(cnt);
                    out.write(cur, 0, 1);
                    cnt = 0;
                }
            } else {
                /* liczbe powtarzajacych sie znakow zapisujemy na jednym bajcie
                wypisz sekwencje jezeli ma ona maksymalna dlugosc */        
                if (cnt == 255) {
                    out.write(prev, 0, 1);
//                    tmp[0] = (byte) cnt;
//                    out.write(tmp, 0, 1);
                    out.write(cnt);
                    cnt = 0;
                    cont = in.read(cur, 0, 1);
                    prev[0] = (byte) ~cur[0];
                    continue;
                } else {
                    /* licz powtarzajace sie znaki */        
                    cnt++; 
                }
            }
            /* odczytaj kolejny znak z pliku wejsciowego */
            cont = in.read(tmp, 0, 1);
            if (cont == 1) {
                prev[0] = cur[0];     
                cur[0] = tmp[0];
            }
        }

        /* jezeli plik konczy sie sekwencja wypisz ja */
        if (prev[0] == cur[0]){
            cnt--;
            out.write(prev, 0, 1);
//            tmp[0] = (byte) cnt;
//            out.write(tmp, 0, 1);
            out.write(cnt);
        }
		
	}

	public void compress(BufferedReader in, BufferedWriter out) throws IOException {
        String resS = "";
        String line;
        try{
        while((line = in.readLine()) != null){
            resS = line;
            String[] resA = resS.split(",");
                for(int i = 0; i < resA.length; i++){
                    if(!resA[i].isEmpty()){
                        if(resA[i].startsWith("0") && resA[i].length() > 1){
                                out.write("0"+resA[i].length()+",");        		
                        } else {
                                out.write(resA[i]+",");
                        }
                    }
            }
                out.newLine();
        }
        out.flush();
        out.close();
        } catch(Exception e){
            e.printStackTrace();
        }
        
        
    }

    @Override
    public void decompress(InputStream in, OutputStream out) throws IOException
    {
        byte cur[] = new byte[1];
        byte prev[] = new byte[1];
        byte cnt[] = new byte[1];
        int  cont, i;

        char[] c = new char[1];
        c[0] = ',';
        char d = (char)',';
        try{
        System.out.println((int)c[0]+" ");
        System.out.println(Character.getNumericValue(d));
        } catch(Exception e){
        	System.out.println(e.getMessage());
        }
        
        cont = in.read(cur, 0, 1);
        prev[0] = (byte) ~cur[0];
//        System.out.println("RLE "+Byte.parseByte(","));

        while (cont == 1) {
            if (prev[0] != cur[0]) {
//                different symbols
                out.write(cur, 0, 1);    
                prev[0] = cur[0];
                
                cont = in.read(cur, 0, 1);
            } else {
                //there is a sequence, read a number of repetitions
                cont = in.read(cnt, 0, 1);
                for(i=0; i<=(char)(cnt[0]&0xFF); i++){
                    out.write(cur, 0, 1);
//                    char[] c = new char[1];
//                    c[0] = ',';
//                    System.out.println((int)c[0]);
//                    out.write((int)c[0]);
                }
                
                cont = in.read(cur, 0, 1);
                prev[0] = (byte) ~cur[0];       
            }
        }
    }
    
    public String decompress(BufferedReader in) throws IOException
    {
    	String sad="";
        String line;
        StringBuilder res = new StringBuilder();
        while((line = in.readLine()) != null){
            sad = line;
            
    	String[] arr = sad.split(",");
    	for(int i = 0; i < arr.length; i++){
    		if(arr[i].startsWith("0") && arr[i].length() > 1){
    			for(int a = 0; a < Integer.parseInt(arr[i].substring(1,arr[i].length())); a++){
    				res.append(0);
                                res.append(",");
    			}
    		} else {
                    res.append(arr[i]);
                    res.append(",");
    		}
    	}
    	
        }
    	
        System.out.println("deco over");
        return res.toString();
    }
    
}
